<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="选择发货申请"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <delivery_request ref="delivery_request"></delivery_request>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
<script>
import VolBox from "@/components/basic/VolBox.vue";
import delivery_request from "@/views/salesinfo/delivery_request/q_delivery_request.vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  components: {
    "vol-box": VolBox,
    delivery_request,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.delivery_request.searchFormFields.AuditStatus = "1";
      await this.$refs.delivery_request.$refs.grid.search();
    },
    open() {
      this.model = true;
    },
    detailOnSelect() {
      let rowObj = this.$refs.delivery_request.$refs.grid.getSelectRows();

      this.$emit("parentCall", ($parent) => {
        if (rowObj.length) {
          let AuditStatusSome = rowObj.some((item) => item.AuditStatus != 1);
          if (AuditStatusSome) {
            this.$message.error("请勿选择审核未通过单据");
            return;
          }
          if (this.$route.path == "/q_install_request") {
            //  let detailRowData = $parent.$refs.detail.rowData;
            //  let num = 0;
            //  detailRowData.forEach((item) => {
            //    rowObj.forEach((o) => {
            //      if (o.Voucher_sub == item.Voucher_sub) num++;
            //    });
            //  });
            //  if (!num) {
            //    let newRowObj = rowObj.map((item) => {
            //      return {
            //        Voucher_sub: item.Voucher_sub,
            //        Client: item.Client,
            //        Goods_num: item.Goods_num,
            //      };
            //    });
            //    this.$emit("parentCall", ($parent) => {
            //      $parent.$refs.detail.rowData.unshift(...newRowObj);
            //    });
            //    return (this.model = false);
            //  } else {
            //    this.$message.error("此采购申请单已包含该商品");
            //  }
            //  return (this.model = false);
          } else if (this.$route.path == "/q_sales") {
            $parent.editFormFields.Voucher_sub = rowObj[0].Voucher_sub;
            $parent.editFormFields.Client = rowObj[0].Client;
            $parent.editFormFields.Goods_num = rowObj[0].Goods_num;
            $parent.editFormFields.Delivery_mode = rowObj[0].Delivery_mode;
            $parent.editFormFields.Delivery_address =
              rowObj[0].Delivery_address;
          }
          this.onModelClose();
          return (this.model = false);
        } else {
          return this.$message.error("请选择数据");
        }
      });
    },
  },
};
</script>